﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using NTTDATA.BEENChina.ResultCertification.model;
using NTTDATA.BEENChina.ResultCertification.Config; 
using NTTDATA.BEENChina.ResultCertification.Main.ServiceResultCertification;
namespace NTTDATA.BEENChina.ResultCertification.UI
{
    public partial class FormCheckConditionB : Form
    {
    
        private CheckSetting setting = null;
        private bool changed = false;
        public FormCheckConditionB()
        {
            InitializeComponent();
            LoadGrid();
        }

        private void LoadGrid ( )
        {
            try
            {
                //setting = (new CheckSettingAccess()).GetCheckSetting();
                using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
                {
                    setting = client.GetCheckSetting(); 
                    fillTree();
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }


        private void fillTree()
        {
            treeGroup.AfterCheck -= this.treeGroup_AfterCheck;
            TreeNode parentNode = treeGroup.Nodes.Add("Cagegory Group");

            string[] fieldinfo = typeof(EnumCategoryType).GetEnumNames();
            foreach (String item in fieldinfo)
            {
                TreeNode node = parentNode.Nodes.Add(item);
                EnumCategoryType enumValue = (EnumCategoryType)Enum.Parse(typeof(EnumCategoryType), item);
                node.Checked = setting.VerifyList.Any(o => o.CategoryType == enumValue && o.Selected == true);
                node.Tag = enumValue;
            }
            treeGroup.Nodes[0].Checked = allChecked(treeGroup.Nodes[0]);
            parentNode.Expand();

            treeGroup.AfterCheck += this.treeGroup_AfterCheck;
        }

       

     

        private void treeGroup_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag == null)
            {
                return;
            }
            EnumCategoryType groupId = (EnumCategoryType)e.Node.Tag;
            fillListView(groupId);
        }

        private void fillListView(EnumCategoryType groupId)
        {
            if (setting.VerifyList == null)
            {
                return;
            }

            try
            {
                listViewCheckItems.ItemChecked -= this.listViewCheckItems_ItemChecked;

                listViewCheckItems.Clear();
                listViewCheckItems.View = View.List;

                List<VerfityItem> list = setting.VerifyList
                                        .Where(o => o.CategoryType == groupId)
                                        .OrderBy(t=>t.VerfityItemId)
                                        .ToList();

                foreach (VerfityItem item in list)
                {
                    ListViewItem lvi = new ListViewItem(new string[] { item.VerfityItemName + "【" + item.VerfityItemId + "】", item.VerfityItemId }, -1);
                    lvi.Tag = item;
                    lvi.Checked = item.Selected;
                    listViewCheckItems.Items.Add(lvi);
                }

                listViewCheckItems.ItemChecked += this.listViewCheckItems_ItemChecked;
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }


        private void treeGroup_AfterCheck(object sender, TreeViewEventArgs e)
        {            
            if (e.Node.Tag == null)
            {
                CheckAllChildNodes(e.Node, e.Node.Checked);
                return;
            }

            EnumCategoryType groupId = (EnumCategoryType)e.Node.Tag;
            setting.VerifyList.Where(o => o.CategoryType == groupId).ToList().ForEach(o => o.Selected = e.Node.Checked);
            changed = true;

            if (treeGroup.SelectedNode!=null && treeGroup.SelectedNode.Tag != null)
            {
                EnumCategoryType curGroupId = (EnumCategoryType)((TreeView)sender).SelectedNode.Tag;
                fillListView(curGroupId);
            } 

            treeGroup.AfterCheck -= this.treeGroup_AfterCheck;
            treeGroup.Nodes[0].Checked = allChecked(treeGroup.Nodes[0]);
            treeGroup.AfterCheck += this.treeGroup_AfterCheck;
        }

        private  void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
        {
            foreach (TreeNode node in treeNode.Nodes)
            {
                node.Checked = nodeChecked;
                if (node.Nodes.Count > 0)
                {
                    this.CheckAllChildNodes(node, nodeChecked);
                }
            }
        }

        private bool allChecked(TreeNode treeNode)
        {
            bool check = false;
            foreach (TreeNode node in treeNode.Nodes)
            {
                if (node.Checked == true)
                {
                    check = true;
                    break;
                }
                else
                {
                    check = allChecked(node);
                }
            }
            return check;
        }
        

        private void listViewCheckItems_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            VerfityItem item = (VerfityItem)e.Item.Tag;
            item.Selected = e.Item.Checked;

            treeGroup.AfterCheck -= this.treeGroup_AfterCheck;

            bool selected = listViewCheckItems.Items.Cast<ListViewItem>().Any(o => o.Checked == true);
            treeGroup.SelectedNode.Checked = selected; ;
            //CheckAllChildNodes(treeGroup.SelectedNode, treeGroup.SelectedNode.Checked);
            treeGroup.Nodes[0].Checked = allChecked(treeGroup.Nodes[0]);

            treeGroup.AfterCheck += this.treeGroup_AfterCheck;

            changed = true;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            { 
           
                using (ServiceResultCertificationClient client = new ServiceResultCertificationClient())
                {
                    if (setting != null)
                    {
                        client.SaveVerifyCategorys(setting.VerifyList);
                    }
                }
                changed = false;

                MessageBox.Show("保存成功");
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.Message);
            }
        }

       


        private void button1_Click(object sender, EventArgs e)
        {
            if (this.changed == true)
            {
                if (MessageBox.Show("放弃修改么?", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                {
                    this.Close();
                }
                return;
            }
            this.Close();
        }

      
    }
}